package main

import (
	"fmt"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"iot/common/sys"
	"os"
)

// 核心框架 - cmd/root.go
func main() {
	sys.InitConfig()
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

var RootCmd = &cobra.Command{
	Use:   "iotadmin",
	Short: "物联网平台控制台",
	Long:  `物联网平台管理工具，支持初始化、导入、控制、查询等功能`,
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		fmt.Println("IoT Platform CLI - Version 1.0")
	},
}

func init() {
	// 全局参数
	RootCmd.PersistentFlags().BoolP("verbose", "v", false, "详细输出模式")
	addInitCommands()
	addControlCommands()
	addImportCommands()
	addQueryCommands()
}

// 命令执行上下文
type CommandContext struct {
	Verbose bool
}

func getContext(cmd *cobra.Command) CommandContext {
	verbose, _ := cmd.Flags().GetBool("verbose")
	return CommandContext{Verbose: verbose}
}

func ConsoleAddr() string {
	return fmt.Sprintf("%s:%s", viper.GetString("console.ip"), viper.GetString("console.port"))
}
